package android.slc.appbase.ui.vd;

import android.slc.adapter.LoadMoreDataFormat3;
import android.slc.appbase.ui.vd.wrapper.IBaseListPageWrapper;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.io.Serializable;
import java.util.List;


/**
 * 基础的分页加载盒子vm
 *
 * @author slc
 * @date 2020-08-25 16:13
 */
public abstract class BaseListPageVd<T extends Serializable, VB extends BaseListCommonVmBox<T>> extends BaseListCommonVd<T, VB>
        implements LoadMoreDataFormat3.LoadModelCallback {

    protected IBaseListPageWrapper<?> getListPageWrapper() {
        if (vmBox instanceof IBaseListPageWrapper<?>) {
            return (IBaseListPageWrapper<?>) vmBox;
        }
        throw new RuntimeException("vmBox没有实现IBaseListPageWrapper接口");
    }

    @Override
    protected void initView() {
        super.initView();
        getListPageWrapper().getLoadMoreDataFormat3().setLoadModelCallback(this);
    }

    protected BaseQuickAdapter<T, BaseViewHolder> initAdapterAndBaseEvent() {
        BaseQuickAdapter<T, BaseViewHolder> reportByAdapter = super.initAdapterAndBaseEvent();
        reportByAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> getListPageWrapper().loadMore());
        return reportByAdapter;
    }

    @Override
    public void refresh(List<?> list) {
        swipeRecycler.refresh();
    }

    @Override
    public void loadMoreEnd() {
        swipeRecycler.loadMoreEnd();
    }

    @Override
    public void loadMoreComplete() {
        swipeRecycler.loadMoreComplete();
    }

    @Override
    public void loadMoreFail() {
        swipeRecycler.loadMoreFail();
    }

    @Override
    public void setRefreshing(boolean refresh) {
        swipeRecycler.setRefreshing(refresh);
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        super.onDestroy(owner);
        getListPageWrapper().getLoadMoreDataFormat3().setLoadModelCallback(null);
    }
}